/*本模块功能:问答(老师)
        编写者:yxj
        编写时间:20180517*/
$(function() {
    var Alert1 = new Alert();
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    var confirm1 = new Confirm();
    function formatDate(now) {//处理时间戳
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        if(month<10){month="0"+month;}
        if(date<10){date="0"+date;}
        if(hour<10){hour="0"+hour;}
        if(minute<10){minute="0"+minute;}
        return year + "-" + month + "-" + date + "   " + hour + ":" + minute;
    }
    var th = '<tr>' +
        '<th  class="col-lg-1"><input type="checkbox" name="controlAll" id="controlAll" class="allCheck"></th>'+
        '<th class="col-lg-3">问题名称</th>' +
        '<th class="col-lg-2">发帖人</th>' +
        '<th class="col-lg-2">学号/工号</th>' +
        '<th class="col-lg-1">发言状态</th>' +
        '<th class="col-lg-3">课程名称</th>' +
        '<th class="col-lg-1">回答数量</th>' +
        '<th class="col-lg-2">时间</th>' +
        '</tr>';
    function tabelModel(opt) {//列表内容
        var time = new Date(opt.problenTime);
        var excuseMark = opt.excuseMark;
        var str = "";
        if(excuseMark == 0){
            str = "<a name="+ opt.userName +" id="+ opt.stuorteaId +" class=\"closeSpeak \" href=\"javascript:void(0)\">禁言</a>";
        }else{
            str="<a class=\"hadSpeak \" href=\"javascript:void(0)\">已禁言</a>"
        }
        return '<tr class="list">' +
            '<td  class="col-lg-1"><input   value=\"0\" id=' + opt.problemId + ' name=\"selecteds\"  type=\"checkbox\" class=\"oneCheck\" /></td>'+
            '<td  class="col-lg-3 detailBtn"    problemid="' + opt.problemId + '"  title='+ opt.problemName +'><a href="javascript:void(0)">' + opt.problemName + '</a></td>' +
            '<td class="col-lg-2" title='+  opt.userName +'><span style="width: 100%;overflow: hidden;text-overflow: ellipsis;" class="fl">' + opt.userName + '</span></td>' +
            '<td class="col-lg-2" title='+ opt.stuorteaId +'><span class="fl">' +opt.stuorteaId + '</span></td>' +
            '<td class="col-lg-1" style="text-align: center">'+ str +'</td>' +
            '<td class="col-lg-3" title='+ opt.courseName +'><span class="fl">' + opt.courseName + '</span></td>' +
            '<td class="col-lg-1"  style="text-align: center"><span>' +opt.problemCount + '</span></td>' +
            '<td class="col-lg-2" title='+  formatDate(time) +' style="text-align: center"><span >' + formatDate(time) + '</span></td>' +
            '</tr>';
    }
    var something;
    var index = {
        some:'',
        init: function() {
            this.bind();
        },
        bind: function() {
            var self = this;
            var json = {
                "pageNow": 1,
                "pageSize": 10
            };

            if(result.paramData!=undefined){
                if(result.paramData.some!=undefined){
                    json = result.paramData.some;
                    if(json.courseId!=undefined){
                        $("#departNames").val(json.courseId)
                    }
                    if(json.problemName!=undefined){
                        $("#keyWord").val(json.problemName)
                    }
                    if(json.userName!=undefined){
                        $("#username").val(json.userName)
                    }
                }
            }
            self.some = json;
            self.postData(json);
            $(".searchBtn").off("click").on("click", function() {
                var departmentId = $("#departNames").val();
                var problemName = $("#keyWord").val();
                var userName = $("#username").val();
                var jsonStr = {
                    "pageNow": 1,
                    "pageSize": 10
                };
                if(departmentId != 0) {
                    jsonStr.courseId = Number(departmentId);
                }
                if(problemName != 0) {
                    jsonStr.problemName = problemName;
                }
                if(userName != 0) {
                    jsonStr.userName = userName;
                }
                self.postData(jsonStr);
                self.some = jsonStr;
                something = jsonStr;
            });
            $(".delBtn").off("click").on("click", function() {
                self.delEvent();
            });
        },
        delEvent:function () {
            var self = this;
            if($(".oneCheck[value=1]").length == 0) {
                Alert1.content = "请选择要删除的问题";
                Alert1.init();
                Alert1.show();
            } else {
                confirm1.content = "确认删除选中的问题吗？";
                confirm1.success = function () {
                    var list = [];
                    for(var i = 0; i < $(".oneCheck[value=1]").length; i++) {
                        list.push(parseInt($(".oneCheck[value=1]").eq(i).attr("id")));
                    }
                    var jsons = JSON.stringify({"list":list});
                    loading1.show();
                    $.ajax({
                        type: "post",
                        url: "delProblem",
                        data: {
                            "jsonStr":jsons
                        },
                        success: function(msg){
                            loading1.hide();
                            msg = JSON.parse(msg);
                            if(msg.result == "success"){
                                Alert1.content = "删除成功";
                                Alert1.success = function () {
                                    self.postData(self.some);
                                };
                                Alert1.init();
                                Alert1.show();

                            }else{
                                if(msg.errorMsg != "" && msg.errorMsg != undefined){
                                    Alert1.content = msg.errorMsg;
                                }else{
                                    Alert1.content = "删除失败";
                                }
                                Alert1.init();
                                Alert1.show();
                            }
                        }
                    });
                };
                confirm1.init();
                confirm1.show();

            }
        },
        skipEvent:function () {
            var self = this;
            $(".closeSpeak").off("click").click(function () {
                var $that = $(this);
                var id = $that.attr("id");
                confirm1.content = "是否对"+ $that.attr("name") +"进行禁言？";
                confirm1.success = function () {
                    var json = JSON.stringify({"excuseMark":1,"list":[id]});
                    $.ajax({
                        url:'excuseProblem',
                        method:'post',
                        async:true,
                        data:{
                            "jsonStr":json
                        },
                        success:function (msg) {
                            msg = JSON.parse(msg);
                            if(msg.result == "success"){
                                Alert1.content = "禁言成功";
                                Alert1.success = function () {
                                    if(something!=undefined){
                                        self.postData(something);
                                    }else{
                                        var json = {
                                            "pageNow": 1,
                                            "pageSize": 10
                                        };
                                        self.postData(json);
                                    }

                                };
                                Alert1.init();
                                Alert1.show();
                            }else{
                                if (msg.errorMsg != "" && msg.errorMsg != undefined) {
                                    Alert1.content = msg.errorMsg;
                                } else {
                                    Alert1.content = "禁言失败";
                                }
                                Alert1.init();
                                Alert1.show();
                            }
                        }
                    });
                };
                confirm1.init();
                confirm1.show();
            });
            $(".detailBtn").off("click").click(function () {
                var json = JSON.stringify({
                    "pageName": "adminCourseInterflowDetail",
                    "paramData":{
                        "isMark":0,
                        "problemId":parseInt($(this).attr("problemid")),
                        "some":self.some
                    }
                });
                window.location.href = "skipPage?jsonStr=" + json;
            });
        },
        postData: function(jsons) { //获得搜索及列表数据
            var self = this;
            var parameter = jsons;
            loading1.show();
            $.ajax({
                type: "post",
                url: "getAdminAllProblemListByPost",
                async: true,
                data: {
                    "jsonStr": JSON.stringify(parameter)
                },
                success: function(data, status, xhr) {
                    loading1.hide();
                    $(".mainContent .tableBox").empty();
                    var msg = JSON.parse(data);
                    if(msg.result == "success"){
                        if(msg.problemList.length != 0) {
                            //列表
                            var tabel = "";
                            $.map(msg.problemList, function(opt) {
                                tabel += tabelModel(opt);
                            });
                            $(".mainContent .tableBox").append(th + tabel);
                            self.allCheckEvent();
                            self.skipEvent();

                        } else {
                            var trImg = "<tr><td colspan='8'><img style='position:relative;margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
                            $(".mainContent .tableBox").append(th + trImg)
                        }

                        var sumpage = msg.totalPage;
                        if(sumpage == 0){
                            sumpage = 1;
                        }
                        var nowpage = 1;
                        var pagenumber = 10;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {

                                nowpage = p;

                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                var jsonStr = jsons;
                                jsonStr.pageNow = nowpage;
                                loading1.show();
                                $.ajax({
                                    type: "post",
                                    url: "getAdminAllProblemListByPost",
                                    async: true,
                                    data: {
                                        "jsonStr": JSON.stringify(parameter)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        loading1.hide();
                                        $(".mainContent .tableBox").empty();
                                        var msg = JSON.parse(msg);
                                        if(msg.problemList.length != 0) {
                                            //列表
                                            var tabel = "";
                                            $.map(msg.problemList, function(opt) {
                                                tabel += tabelModel(opt);
                                            });
                                            $(".mainContent .tableBox").append(th + tabel);
                                            self.allCheckEvent();
                                            self.skipEvent();

                                        } else {
                                            var trImg = "<tr><td colspan='8'><img style='position:relative;margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
                                            $(".mainContent .tableBox").append(th + trImg)
                                        }
                                        //获取课程
                                        $("#departNames").html("<option value=\"0\">请选择</option>");
                                        $.map(msg.courseList, function(opt) {
                                            $("#departNames").append("<option value=" + opt.courseId + ">" + opt.courseName + "</option>");
                                        });
                                        $("#departNames").val(self.some.courseId!=undefined?self.some.courseId:0);
                                        if(msg.searchObejct != null){
                                            if(msg.searchObejct.courseId!=undefined){
                                                $("#departNames").val(msg.searchObejct.courseId);
                                            }
                                            if(msg.searchObejct.problemName!=undefined){
                                                $("#keyWord").val(msg.searchObejct.problemName);
                                            }
                                            if(msg.searchObejct.userName!=undefined){
                                                $("#username").val(msg.searchObejct.userName);
                                            }
                                        }
                                    },
                                    error: function(data, status, xhr) {
                                        loading1.hide();
                                        var json = JSON.stringify({
                                            "pageName": "error"
                                        });
                                        window.location.href = "skipPage?jsonStr=" + json;

                                    }
                                });
                            }
                        });
                        //获取课程
                        $("#departNames").html("<option value=\"0\">请选择</option>");
                        $.map(msg.courseList, function(opt) {
                            $("#departNames").append("<option value=" + opt.courseId + ">" + opt.courseName + "</option>");
                        });
                        $("#departNames").val(self.some.courseId!=undefined?self.some.courseId:0);
                        if(msg.searchObejct != null){
                            if(msg.searchObejct.courseId!=undefined){
                                $("#departNames").val(msg.searchObejct.courseId);
                            }
                            if(msg.searchObejct.problemName!=undefined){
                                $("#keyWord").val(msg.searchObejct.problemName);
                            }
                            if(msg.searchObejct.userName!=undefined){
                                $("#username").val(msg.searchObejct.userName);
                            }
                        }

                    }else {
                        if(msg.failMsg != undefined){
                            if(msg.failMsg == "暂无权限"){
                                parent.location.reload(true);
                            }else{
                                Alert1.content = msg.failMsg;
                                Alert1.init();
                                Alert1.show();
                            }
                        }else{
                            if (msg.errorMsg != "" && msg.errorMsg != undefined) {
                                Alert1.content = msg.errorMsg;
                            } else {
                                Alert1.content = "获取失败";
                            }
                            Alert1.init();
                            Alert1.show();
                        }


                    }

                },
                error: function(data, status, xhr) {
                    loading1.hide();
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        },
        allCheckEvent: function() {
            //全选
            allCheck.init("controlAll", "oneCheck");
        }

    };
    index.init();

});